package com.atguigu.tingshu.user.login;

import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class LoginClient implements ApplicationContextAware {

    private static final Map<Integer, ILogin> MAP=new ConcurrentHashMap<>();
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
         Map<String, Object> beanMap = applicationContext.getBeansWithAnnotation(LoginBean.class);
        if (CollectionUtils.isEmpty(beanMap)){
            return;
        }
        //获取bean集合
        Collection<Object> beans = beanMap.values();
        beans.forEach(bean -> {
            LoginBean loginBean = bean.getClass().getAnnotation(LoginBean.class);
                MAP.put(loginBean.value(), (ILogin) bean);
        });
    }

    public Map<String,Object> login(Integer type,LoginParam param) throws Exception {
        return MAP.get(type).login(param);
    }
}
